package com.yaxin.mq.config;

import com.yaxin.mq.callback.RabbitCallback;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author lgj
 * @version 1.0
 * @description TODO
 * @date 2025/7/18 15:40
 */
@Configuration
public class RabbitTemplateConfig {

    @Resource
    private RabbitCallback rabbitCallback;  // 注入你的回调组件

    /**
     * 配置消息转换器为 JSON
     */
//    @Bean
//    public MessageConverter messageConverter() {
//        return new Jackson2JsonMessageConverter();
//    }

    /**
     * 配置 RabbitTemplate，绑定回调+转换器
     */
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setConfirmCallback(rabbitCallback);
        rabbitTemplate.setReturnsCallback(rabbitCallback);
        rabbitTemplate.setMandatory(true);
        //rabbitTemplate.setMessageConverter(messageConverter);
        rabbitCallback.setRabbitTemplate(rabbitTemplate); // 反向注入，避免循环依赖
        return rabbitTemplate;
    }

}

